wx test.py (3206B)
1 ### WX TEST ### 2 # Copyright David Barker 2010 3 # 4 # A simple test of using wxPython to create a gui for PyIgnition 5 6 7 import wx, sys, os, pygame, PyIgnition 8 9 class Frame(wx.Frame): 10 def __init__(self, parent): 11 wx.Frame.__init__(self, parent, -1) 12 self.panel = wx.Panel(self, -1) 13 self.hwnd = self.GetChildren()[0].GetHandle() 14 os.environ['SDL_VIDEODRIVER'] = 'windib' 15 os.environ['SDL_WINDOWID'] = str(self.hwnd) 16 self.statusbar = self.CreateStatusBar() 17 self.statusbar.SetFieldsCount(3) 18 self.statusbar.SetStatusWidths([-3, -4, -2]) 19 self.statusbar.SetStatusText("ExeSoft Obsidian", 0) 20 self.statusbar.SetStatusText("Look, it's a nifty status bar!!!", 1) 21 pygame.display.init() 22 self.screen = pygame.display.set_mode() 23 self.Bind(wx.EVT_PAINT, self.Draw) 24 self.Bind(wx.EVT_SIZE, self.OnSize) 25 self.Bind(wx.EVT_CLOSE, self.Kill) 26 self.val = 0 27 self.SetTitle("ExeSoft Obsidian") 28 self.size = self.panel.GetSizeTuple() 29 self.effect = PyIgnition.ParticleEffect(self.screen, (0, 0), self.size) 30 self.source = self.effect.CreateSource((self.size[0] / 2, self.size[1]), initspeed = 5.0, initdirection = 0.0, initspeedrandrange = 2.0, initdirectionrandrange = 0.2, particlesperframe = 10, particlelife = 200, drawtype = PyIgnition.DRAWTYPE_POINT, colour = (255, 255, 200)) 31 self.grav = self.effect.CreateDirectedGravity(0.0, 0.2, [1, 0]) 32 self.gravslider = wx.Slider(self, wx.ID_ANY, 0, -50, 50, style = wx.SL_HORIZONTAL | wx.SL_LABELS) 33 self.gravslider.SetTickFreq(0.1, 1) 34 self.Bind(wx.EVT_SCROLL, self.OnScroll) 35 sizer = wx.BoxSizer(wx.VERTICAL) 36 sizer.Add(self.gravslider, 0, flag = wx.EXPAND) 37 sizer.Add(self.panel, 1, flag = wx.EXPAND) 38 self.SetAutoLayout(True) 39 self.SetSizer(sizer) 40 self.Layout() 41 42 def Kill(self, event): 43 # Make sure Pygame can't be asked to redraw /before/ quitting 44 # (Otherwise wx seems to call Draw between quitting Pygame and destroying the frame) 45 self.Unbind(event = wx.EVT_PAINT, handler = self.Draw) 46 pygame.quit() 47 self.Destroy() 48 49 def OnSize(self, event): 50 self.Layout() 51 self.size = self.panel.GetSizeTuple() 52 self.source.CreateKeyframe(self.source.curframe, pos = (self.size[0] / 2, self.size[1])) 53 54 def Draw(self, event): 55 self.screen.fill((0, 0, 0)) 56 self.effect.Update() 57 self.effect.Redraw() 58 pygame.display.update() 59 60 self.val += 1 61 self.statusbar.SetStatusText("Frame %i" % self.val, 2) 62 #self.statusbar.Refresh() 63 #self.gravslider.Refresh() 64 self.OnPaint(event) 65 66 def OnScroll(self, event): 67 self.grav.CreateKeyframe(self.grav.curframe, strength = float(self.gravslider.GetValue()) / 100.0) 68 69 class App(wx.App): 70 def OnInit(self): 71 self.frame = Frame(parent = None) 72 self.frame.Show() 73 self.SetTopWindow(self.frame) 74 75 return True 76 77 if __name__ == "__main__": 78 app = App() 79 app.MainLoop()